/**
 * ...
 * @author Rob Dravenstott
 */
import com.chumby.WidgetParams;
import com.chumby.util.Delegate;
class Main 
{
	private static var _widgetParams:WidgetParams = null;
	private static var _appRoot:MovieClip;

	public static function main(swfRoot:MovieClip):Void 
	{
		_appRoot = swfRoot;
		var app = new Main(swfRoot);
	}
	
	public function Main() 
	{
		_quality = "high";

		// Set the scale mode locked so we can resize to stage properly
		Stage.scaleMode = "noScale";
		Stage.align = "TL";
		
		// Display loading message on opening screen
		WeatherRadar.showTextMessage("LOADING...", 32);
				
		_widgetParams = new WidgetParams();
		_widgetParams.addEventListener(WidgetParams.GOT_PARAMS,Delegate.create(_root, gotParameters));
		_widgetParams.addEventListener(WidgetParams.SENT_PARAMS,Delegate.create(_root, sentParameters));
		_widgetParams.getWidgetParams();
	}
	private function gotParameters(event:Object) 
	{
		var settings = new Object();
		
		if (!event.status) 
		{
			trace("Could not load parameters, using defaults.");
		} 	
		else
		{
			//Read the chumby parameters
			var parameters:Object = _widgetParams.chumbyParams();
			
			if (parameters["radar_zoom"] != undefined) 
				settings.radar_zoom = parameters["radar_zoom"];
			if (parameters["radar_type"] != undefined)
				settings.radar_type = parameters["radar_type"];
			if (parameters["radar_max_images"] != undefined)
				settings.radar_max_images = parameters["radar_max_images"];
			if (parameters["radar_id"] != undefined)
				settings.radar_id = parameters["radar_id"];
			if (parameters["calc_x"] != undefined)
				settings.calc_x = parameters["calc_x"];
			if (parameters["calc_y"] != undefined)
				settings.calc_y = parameters["calc_y"];
		}

		if (_root["_chumby_screen_width"] != undefined)
			settings.max_photo_width = _root["_chumby_screen_width"];
		if (_root["_chumby_screen_height"] != undefined)
			settings.max_photo_width = _root["_chumby_screen_height"];
		
		// Check to make sure we didn't pass in an override via html flashvars appurl param
		if (_level0.radar_zoom != undefined)
			settings.radar_zoom = _level0.radar_zoom;
		if (_level0.radar_type != undefined)
			settings.radar_type = _level0.radar_type;
		if (_level0.radar_max_images != undefined)
			settings.radar_max_images = _level0.radar_max_images;
		if (_level0.radar_id != undefined)
			settings.radar_id = _level0.radar_id;
		if (_level0.calc_x != undefined)
			settings.calc_x = _level0.calc_x;
		if (_level0.calc_y != undefined)
			settings.calc_y = _level0.calc_y;
			
		//Run the actual presentation code
		WeatherRadar.run(_appRoot, settings, Main.saveNewSettings);
	}
		
		// Save the data to the server
	public static function saveNewSettings (settings:Object) 
	{
		var parameters:Object = new Object();
				
		parameters["radar_zoom"] = String(settings.radar_zoom);
		parameters["radar_type"] = String(settings.radar_type);
		parameters["radar_max_images"] = String(settings.radar_max_images);
		parameters["radar_id"] = String(settings.radar_id);
		if (settings.calc_x != undefined)
			parameters["calc_x"] = String(settings.calc_x);
		if (settings.calc_y != undefined)
			parameters["calc_y"] = String(settings.calc_y);

		// Only save if we're on a chumby
		if (_widgetParams != null)
		{
			_widgetParams.sendWidgetParams(parameters);
		}
		else
		{
			trace("Not saving, Not a Chumby");
		}
	}

	// called as a callback when done saving the data to the server
	private function sentParameters(event:Object) 
	{
		if (!event.status) 
		{
			trace("Error sending parameters.");
			return;
		}
		else
		{
			trace("Saved");
		}
	}
}